package org.bing.flex.action {
    import mx.managers.CursorManager;
    import mx.rpc.IResponder;
    import mx.rpc.events.ResultEvent;

    public class GraniteResponder implements IResponder {

        private var _onResult:Function;
        private var _onFault:Function;
        private var _loading:Boolean;

        public function GraniteResponder(onResult:Function, onFault:Function=null, showLoading:Boolean=false) {
            this._onResult=onResult;
            this._onFault=onFault;
            this._loading=showLoading;
            startLoading();
        }


        public function fault(info:Object):void {
            stopLoading();
            trace("ERR " + info);
        }


        public function result(data:Object):void {
            stopLoading();
            if (_onResult != null) {
                var evt:ResultEvent=data as ResultEvent;
                _onResult(evt.token.result);
            } else {
                trace("RES " + data);
            }
        }

        private function startLoading():void {
            if (_loading) {
                CursorManager.setBusyCursor();
                trace("loading...");
            }
        }

        private function stopLoading():void {
            CursorManager.removeBusyCursor();
            if (_loading) {
                trace("loaded. ");
            }
        }
    }
}